package com.financing.wap.web.controller.api;

import java.util.Collections;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.financing.wap.common.Pager;
import com.financing.wap.common.ResultMap;
import com.financing.wap.common.SessionUtil;
import com.financing.wap.dto.FpShopProjectInformationDTO;
import com.financing.wap.dto.UserDTO;
import com.financing.wap.service.IFpShopProjectInformationService;
import com.financing.wap.util.DTOdateSetUtil;
import com.financing.wap.web.controller.BaseController;

@Controller("fpShopProjectInformationController")
@RequestMapping("/api/fpShopProjectInformation")
public class FpShopProjectInformationController extends BaseController  { 

	private Logger logger = Logger.getLogger(getClass());
	
	@Autowired
	private IFpShopProjectInformationService fpShopProjectInformationService;
	
	@RequestMapping("/listFpShopProjectInformation")
	@ResponseBody
	public ResultMap list(HttpServletRequest request, FpShopProjectInformationDTO fpShopProjectInformation,Pager pager){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("query fpShopProjectInformation list begin .......");
			
//			fpShopProjectInformation.setBegin(pager.getStartIndex());
//			fpShopProjectInformation.setPageSize(pager.getPageSize());
			
			int count = fpShopProjectInformationService.queryListCount(fpShopProjectInformation);
			
			pager.setTotalCount(count);
			
			List<FpShopProjectInformationDTO> fpShopProjectInformationList = null;
			
			if(count > 0){
				fpShopProjectInformationList = fpShopProjectInformationService.queryList(fpShopProjectInformation);
			}else{
				fpShopProjectInformationList = Collections.emptyList();
			}
			result.setAttribute("pager", pager);
			result.setAttribute("list", fpShopProjectInformationList);
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("query fpShopProjectInformation list end .......");
		}catch(Exception e){
			log.error("query fpShopProjectInformation error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
	@RequestMapping("/addFpShopProjectInformation")
	@ResponseBody
	public ResultMap add(HttpServletRequest request, FpShopProjectInformationDTO fpShopProjectInformation){
		ResultMap result = new ResultMap();
		
		try{	
			
			/*if(fpShopProjectInformation.getInformationId()==null ){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}*/
			
			logger.info("add fpShopProjectInformation  begin .......");
			UserDTO user = SessionUtil.getSession().getUser();
			DTOdateSetUtil.setCreateInfo(fpShopProjectInformation);
			DTOdateSetUtil.setUpdateInfo(fpShopProjectInformation);
			
			fpShopProjectInformation.setULoginName(user.getLoginName());
			//判断是否首页添加
//			if(fpShopProjectInformation.getInformationId()==null){
//				fpShopProjectInformation.setInformationId(0);  //设置为0
//			}
			
			
			//查询删除
			FpShopProjectInformationDTO fp = new FpShopProjectInformationDTO();
			fp.setULoginName(fpShopProjectInformation.getULoginName());
			//对应ID
//			fp.setInformationId(fpShopProjectInformation.getInformationId());
//			if(fpShopProjectInformation.getInformationId()==0){
//				fp.setInformationId(0);  //删除
//				fpShopProjectInformation.setInformationId(null);
//			}
			List<FpShopProjectInformationDTO> ffs= fpShopProjectInformationService.queryList(fp);
			if(ffs.size()>0){
				fp.setDeleteTag("2");
				fpShopProjectInformationService.update(fp);
			}
			
			
			int id = fpShopProjectInformationService.insert(fpShopProjectInformation);
			result.setAttribute("id", id);
			
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("add fpShopProjectInformation list end .......");
		}catch(Exception e){
			e.printStackTrace();
			log.error("add fpShopProjectInformation error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
	
	@RequestMapping("/detailFpShopProjectInformation")
	@ResponseBody
	public ResultMap detail(HttpServletRequest request, FpShopProjectInformationDTO fpShopProjectInformation){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("query detail fpShopProjectInformation  begin .......");
			
			Integer id = fpShopProjectInformation.getId();
			
			if(id==null){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			FpShopProjectInformationDTO dto = fpShopProjectInformationService.loadById(id);
			
			result.setAttribute("dto",dto);
			
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("query detail fpShopProjectInformation  end .......");
		}catch(Exception e){
			log.error("query detail fpShopProjectInformation error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
	@RequestMapping("/deleteFpShopProjectInformation")
	@ResponseBody
	public ResultMap delete(HttpServletRequest request, FpShopProjectInformationDTO fpShopProjectInformation){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("delete fpShopProjectInformation  begin .......");
			
			Integer id = fpShopProjectInformation.getId();
			
			if(id==null){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			List<FpShopProjectInformationDTO> ffs= fpShopProjectInformationService.queryList(fpShopProjectInformation);
			if(ffs.size()!=1){
				result.setStatusCode(ResultMap.STATUS_CODE_OBJECT_NOFOUNT);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			fpShopProjectInformation.setULoginName(ffs.get(0).getULoginName());
//			fpShopProjectInformation.setInformationId(ffs.get(0).getInformationId());
			fpShopProjectInformation.setDeleteTag("2");  //设置删除 
			fpShopProjectInformationService.update(fpShopProjectInformation);
			
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("delete fpShopProjectInformation  end .......");
		}catch(Exception e){
			log.error("delete fpShopProjectInformation error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
	@RequestMapping("/updateFpShopProjectInformation")
	@ResponseBody
	public ResultMap update(HttpServletRequest request, FpShopProjectInformationDTO fpShopProjectInformation){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("update fpShopProjectInformation  begin .......");
			
			Integer id = fpShopProjectInformation.getId();
			
			if(id==null){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			DTOdateSetUtil.setUpdateInfo(fpShopProjectInformation);
			
			fpShopProjectInformationService.update(fpShopProjectInformation);
			
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("update fpShopProjectInformation list end .......");
		}catch(Exception e){
			log.error("update fpShopProjectInformation error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
	@RequestMapping("/updateForceFpShopProjectInformation")
	@ResponseBody
	public ResultMap updateForce(HttpServletRequest request, FpShopProjectInformationDTO fpShopProjectInformation){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("updateForce fpShopProjectInformation  begin .......");
			
			Integer id = fpShopProjectInformation.getId();
			
			if(id==null){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			DTOdateSetUtil.setUpdateInfo(fpShopProjectInformation);
			
			fpShopProjectInformationService.updateForce(fpShopProjectInformation);
			
			
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("updateForce fpShopProjectInformation list end .......");
		}catch(Exception e){
			log.error("updateForce fpShopProjectInformation error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
}
